<?php

namespace App\Http\Controllers\System;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Validator;
use App\Http\ClassBox\globals;
class CompanyInfoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    
    public function index(Request $request)
    {
        
    
        $Com_list=DB::select('select * from mycom where Type=4  and Del=1');//查询所有的公司
        if($Com_list!=null){
        $Dep_list=DB::select('select * from mycom where Type=6 and Del=1 and ComId='.$Com_list[0]->id);//查询所有的部门
        }else{
             $Dep_list=null;
        }
        
        static $typeTree=array();

        $company_Tree= $this->getTree($Pid = 0,$typeTree);
      
        return view('system/companyInfo',[
                'Com_list'=>$Com_list,
                'Dep_list'=>$Dep_list,
                'company_Tree'=>$company_Tree
        ]);
     
    }
  /*
   * 
   * select 请求
   * deng
   */
    public function select_List(Request $request){
              $globals = new globals();
              
              $ComId=$request->input('ComId');
               if($ComId){
                $company_list=DB::table('mycom')->select('id','Name','Type')->where('ComId',$request->input('ComId'))->where('Del',1)->where('Type',6)->get();//返回部门  select
               }
               
          
            return  $globals->Return_Url($company_list);//调用公共返回方式
        }
        
        
    
    
    /**
     * 添加 |修改       公司  、 部门  、组  ----接口
     *deng
     * @return \Illuminate\Http\Response
     */
    public function create(Requests\CompanyInfoRequest $request)

    {    
         $result=array();
         $globals = new globals();
         $Useid=$globals->UseSession("id");//获取用户的id
       switch($request->input('Type'))
        {
            case 4://代表公司
           
            $requ_name= DB::table('mycom')->where('Type',4)->where('Name',trim($request->input('Name')))->get();
            if($requ_name!=Null){  // 判断公司名称是否存在 
                    return redirect('system/companyInfo/index')->with('Name','1');
           
            }

            if($request->input('id') > 0){

                    DB::table('mycom')->where('id','=',$request->input('id'))->update(
                    array(
                    'Name' =>trim($request->input('Name'))
                    )
                    );
                }else{
                    $insert=array(
                            'Name'=>trim($request->input('Name')),
                            'Type'=>intval($request->input('Type')),
                            'ComId'=>0,
                            'DepId'=>0,
                            'Useid'=>$Useid,
                            'Time'=>date('Y-m-d H:i:s',time()),
                            'Del'=>1,
                            'Pid'=>0
                    );
                  
                    DB::table('mycom')->insert($insert);
                
                    
                }
            break;
            case 6://代表部门
                   $Name=trim($request->input('Name'));
                   $ComId=intval($request->input('ComId'));
                   $requ_name= DB::table('mycom')->where('Type',6)->where('Name',$Name)->where('ComId',$ComId)->get();
                   if($requ_name!=Null){  // 判断部门名称是否存在 
                    return redirect('system/companyInfo/index')->with('Name','2');
           
                   }

                  $validator = Validator::make(  //表单验证
                     $requests=array(
                             'ComId'=>$request->input('ComId')
                           
                    ),
                  $rules= array(
                              'ComId' => 'required'
                             
                    ),
                  $messages=array(
                              'ComId.required'=>'公司不能为空！'
                         
                             
                      )
                
                 );
                if ($validator->fails()) {
                return redirect()->back()->withErrors($validator);
                     
                }

                if($request->input('id') > 0){  //修改部门
                    DB::table('mycom')->where('id','=',$request->input('id'))->update(
                    array(
                    'Name' =>$Name,
                    'ComId'=>$ComId,
                    'Pid'=>$ComId
                    )
                    );

                    DB::table('mycom')->where('DepId','=',$request->input('id'))->update( // 修改部门下的组
                    array(
                    'ComId'=>intval($request->input('ComId'))
                    )
                    );

                    
                }else{
                    $insert=array(
                            'Name'=>$Name,
                            'Type'=>intval($request->input('Type')),
                            'ComId'=>$ComId,
                            'DepId'=>0,
                            'Useid'=>$Useid,
                            'Time'=>date('Y-m-d H:i:s',time()),
                            'Del'=>1,
                            'Pid'=>intval($request->input('ComId'))
                    );

                  
                    DB::table('mycom')->insert($insert);
                    
                }

            break;
            case 8:// 代表组   


                    $Name=trim($request->input('Name'));
                    $ComId=intval($request->input('ComId'));
                    $DepId=intval($request->input('DepId'));
                   $requ_name= DB::table('mycom')->where('Type',8)->where('Name',$Name)->where('ComId',$ComId)->where('DepId',$DepId)->get();
                   if($requ_name!=Null){  // 判断组名称是否存在  
                      return redirect('system/companyInfo/index')->with('Name','3');
           
                   }

                $validator = Validator::make(  //表单验证
                     $requests=array(
                             'ComId'=>$request->input('ComId'),
                             'DepId'=>$request->input('DepId')
                    ),
                  $rules= array(
                              'ComId' => 'required',
                              'DepId' => 'required'
                    ),
                  $messages=array(
                              'ComId.required'=>'公司不能为空！',
                              'DepId.required'=>'部门不能为空！'
                      )
                
                 );
                if ($validator->fails()) {
                return redirect()->back()->withErrors($validator);
                     
                }
            if($request->input('id') > 0){

                    DB::table('mycom')->where('id','=',$request->input('id'))->update(
                    array(
                    'Name' =>$Name,
                    'ComId'=>$ComId,
                    'DepId'=>$DepId,
                    'Pid'=>$DepId
                    )
                    );  

             } else{
                $insert=array(
                        'Name'=>$Name,
                        'Type'=>$request->input('Type'),
                        'ComId'=>$ComId,
                        'DepId'=>$DepId,
                        'Useid'=>$Useid,
                        'Time'=>date('Y-m-d H:i:s',time()),
                        'Del'=>1,
                        'Pid'=>$DepId
                );
            DB::table('mycom')->insert($insert);
                
            }
  
            break;
            
        }
        return redirect('system/companyInfo/index')->with('aa','1');
    }
  
    
    /*
     * 
     * 恢复|停用    ---接口
     * 
     * 0-停用        1-恢复
     * deng
     */
    
   public function restore_disable(Request $request){
         $id=$request->input('id');
         $del=$request->input('del');
         $result=array();

       switch($request->input('Type'))
        {
         case 4:
          if($id!=NULL && $del=='1'){
             DB::table('mycom')->where('id','=',$id)->update(
             array(
               'Del' =>0
                )
               );

             DB::table('mycom')->where('ComId','=',$id)->update(
             array(
               'Del' =>0
                )
               );
            }

            if($id!=NULL && $del=='0'){
                DB::table('mycom')->where('id','=',$id)->update(
                array(
                'Del' =>1
                )
                );
            }
            break;
            case 6:
            if($id!=NULL && $del=='1'){
             DB::table('mycom')->where('id','=',$id)->update(
             array(
               'Del' =>0
                )
               );

             DB::table('mycom')->where('DepId',$id)->update(
             array(
               'Del' =>0
                )
               );
            }
            if($id!=NULL && $del=='0'){
                $S_Del=DB::table('mycom')->select('Del')->where('id',DB::table('mycom')->where('id',$id)->pluck('ComId'))->get();
                /*
                * 查询 当前 部门的上一级是否启用
                * 如果是启用  则可以 修改
                * 如果是停用 则不能  修改 并返回相关提示
                */
                if($S_Del[0]->Del=='1'){
                DB::table('mycom')->where('id','=',$id)->update(
                array(
                'Del' =>1
                )
                );
                }else{
                   $result['flase'] = 'flase';
                   return json_encode($result);

                }

            }
         
            break;
            case 8:
            if($id!=NULL && $del=='1'){
             DB::table('mycom')->where('id','=',$id)->update(
             array(
               'Del' =>0
                )
               );
            }

            if($id!=NULL && $del=='0'){
                $S_Del=DB::table('mycom')->select('Del')->where('id',DB::table('mycom')->where('id',$id)->pluck('DepId'))->get();
                /*
                * 查询 当前 组的上一级是否启用
                * 如果是启用  则可以 修改
                * 如果是停用 则不能  修改 并返回相关提示
                */
                if($S_Del[0]->Del=='1'){
                DB::table('mycom')->where('id','=',$id)->update(
                array(
                'Del' =>1
                )
                );
                }else{
                   $result['flase'] = 'flase';
                   return json_encode($result);

              }
            }
            if($id!=NULL && $del=='0'){
                DB::table('mycom')->where('id','=',$id)->update(
                array(
                'Del' =>1
                )
                );
            }
            break;

        }

        $result['Success'] = 'true';
          $mycom=DB::table('mycom')->select('id','Name','Type')->where('Type',4)->where('Del',1)->get();
          $result['data'] =$mycom;
        return json_encode($result);
      }
    
    

     
    
    
    /**
     *公司展示列表-- 树形
     *deng
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getTree($Pid= 0,$typeTree){
        $result=DB::select('select * from mycom where Pid='.$Pid);
     
        foreach ($result as $list_full){  

             array_push($typeTree,$list_full);
             
         $typeTree=self::getTree($list_full->id,$typeTree);
             
        }
       return $typeTree;
      
   }


  /**
  *  修改按钮接口
  *
  *  deng
  */
   public function edit(Request $request)
    {
        $Id=$request->input('Id');
        switch($request->input('Type'))
        {
            case 4:
            $data['data']=DB::table('mycom')->select('Name','id')->where('id',$Id)->get();
            return json_encode($data);
            break;
            case 6:
            $data['ComId']=DB::table('mycom')->select('Name','id')->where('id',DB::table('mycom')->where('id',$Id)->pluck('ComId'))->where('Type',4)->get();
            $data['data']=DB::table('mycom')->select('ComId','Name','id')->where('id',$Id)->get();
            return json_encode($data);
            break;
            case 8:
            $data['ComId']=DB::table('mycom')->select('Name','id')->where('id',DB::table('mycom')->where('id',$Id)->pluck('ComId'))->where('Type',4)->get();
            $data['DepId']=DB::table('mycom')->select('Name','id')->where('id',DB::table('mycom')->where('id',$Id)->pluck('DepId'))->where('Type',6)->get();
            $data['data']=DB::table('mycom')->select('ComId','DepId','Name','id')->where('id',$Id)->get();
            return json_encode($data);
            break;
            default:
            return 0;
        }
    }
    
}
